/* 
 * AndFollowerStat
 * 
 * Copyright (C) 2009  Daniel Czerwonk <d.czerwonk@googlemail.com>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package de.dan_nrw.android.followerstat.client.followers;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import de.dan_nrw.android.followerstat.Follower;
import de.dan_nrw.android.followerstat.R;
import de.dan_nrw.android.followerstat.dao.folllowers.ITwitterFollowerDAO;

/**
 * @author Daniel Czerwonk
 *
 */
public class FollowerListAdapter extends BaseAdapter {

	private final Context context;
	private final Follower[] followers;
	private final FollowerImageUpdateTaskManager imageUpdateTaskManager;

	
	/**
	 * Creates a new instance of FollowerListAdapter.
	 * @param context
	 * @param followers
	 * @param followerDAO
	 */
	public FollowerListAdapter(Context context, Follower[] followers, ITwitterFollowerDAO followerDAO) {
	    super();
	    
	    this.context = context;
	    this.followers = followers;
	    
	    this.imageUpdateTaskManager = new FollowerImageUpdateTaskManager(followerDAO);
    }


    /* (non-Javadoc)
	 * @see android.widget.Adapter#getCount()
	 */
	@Override
	public int getCount() {
		return this.followers.length;
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getItem(int)
	 */
	@Override
	public Follower getItem(int position) {
		return this.followers[position];
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getItemId(int)
	 */
	@Override
	public long getItemId(int position) {
		return position;
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup viewGroup) {
		final Follower follower = this.followers[position];

		LayoutInflater layoutInflater = LayoutInflater.from(this.context);
		
    	convertView = layoutInflater.inflate(R.layout.follower, null);
		
		convertView.setOnClickListener(new OnClickListener() {

			/* (non-Javadoc)
             * @see android.view.View.OnClickListener#onClick(android.view.View)
             */
            @Override
            public void onClick(View v) {
            	Uri uri = Uri.parse(follower.getProfileUri().toString()); 
            	
            	context.startActivity(new Intent(Intent.ACTION_VIEW, uri));
            }
		});
		
		TextView textView = (TextView)convertView.findViewById(R.id.followerTextView);
		
		textView.setText(follower.getDisplayName());
		
		final ImageView imageView = (ImageView)convertView.findViewById(R.id.followerImageView);
		
		this.imageUpdateTaskManager.updateFollowerImage(imageView, follower);
		
		return convertView;
	}
	
	public void dispose() {
		this.imageUpdateTaskManager.dispose();
	}
}